package com.gs.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;


public class UploadAction extends BasicAction {
	private static final long serialVersionUID = 3254056596367050615L;

	private File file;

	private List<String> fileName;

	private List<String> fileContentType;
	
	private String savePath;

	private long fileSize;
	
	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String upload() throws Exception {
		if (null == getFileName()) {
			return INPUT;
		}
		String root = getSavePath();
		System.out.println("root:"+root);
		File dir=new File(root);
		if(!dir.exists())
			dir.mkdir();
		int fileSize = getFileName().size();
		for (int i = 0; i < fileSize; i++) {
			System.out.println("filePath:"+file.getPath());
			InputStream is = new FileInputStream(file);

			OutputStream os = new FileOutputStream(new File(root, fileName
					.get(i)));

			byte[] buffer = new byte[1024];
			int len;
			while ((len = is.read(buffer)) > 0) {
				os.write(buffer, 0, len);
			}
			os.flush();
			os.close();
			is.close();
		}
		return SUCCESS;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public List<String> getFileName() {
		return fileName;
	}

	public void setFileFileName(List<String> fileName) {
		this.fileName = fileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public long getFileSize() {
		return fileSize;
	}
}
